<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class user extends MY_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');	
		$this->load->model('role_model');	
	}
	
	public function index()
	{
		$this->load->view('admin/user/main');
	}
	
	
			
	public function save()
	{
		$this->form_validation->set_message('is_unique', "The<b> %s </b>already Exist!");
		$this->form_validation->set_message('is_unique2', "The<b> %s </b>already Exist!");
		
		//check if it new record or update to set the validation rules
		$id = $this->input->post('id');
		//insert
		if(empty($id) && $id=='')
		{
		$this->form_validation->set_rules('username', 'User Name' , 'trim|required|min_length[3]|max_length[255]|is_unique[user.username]');
		$this->form_validation->set_rules('email'	, 'E-mail' , 'valid_email|trim|required|min_length[3]|max_length[255]|is_unique[user.email]');
		}
		
		//update
		if($id && !empty($id))
		{
		$this->form_validation->set_rules('username', 'User Name' , 'trim|required|min_length[3]|max_length[255]|is_unique2[user.username.id.'.$id.']');
		$this->form_validation->set_rules('email'	, 'E-mail', 'valid_email|trim|required|min_length[3]|max_length[255]|is_unique2[user.email.id.'.$id.']');
		}
				
		$this->form_validation->set_rules('first_name'	, 'First Name'		, 'trim|required|min_length[3]');		
		$this->form_validation->set_rules('last_name'	, 'Last Name'		, 'trim|required|min_length[3]');		
		$this->form_validation->set_rules('role_id'		, 'User Role'		, 'trim|required|is_natural_no_zero');				
		$this->form_validation->set_rules('password'	, 'Password'		, 'trim|required');		
		$this->form_validation->set_rules('repassword'	, 'Re-password'		, 'matches[password]|trim|required');		
		if ($this->form_validation->run() == FALSE)
		{
			$data['error']= true;
			$data['msg']  = $this->form_validation->getErrorsArray();			
			die(json_encode($data));
			//die(json_encode($errors));
		}
		else
		{
			$form['first_name'] = ucwords(strtolower($this->input->post('first_name')));
			$form['last_name'] 	= ucwords(strtolower($this->input->post('last_name')));
			$form['email'] 		= $this->input->post('email');
			$form['role_id'] 	= $this->input->post('role_id');
			$form['password'] 	= $this->input->post('password');
			$form['username'] 	= $this->input->post('username');
			
			//NEW/Insert
			if($this->input->post('id')=='')
			{
				$form['created_on'] 	= date("Y-m-d H:i:s");
				
				if($this->user_model->insert($form))
				{
					$data['error']= false;
					$data['msg']  = '<span style="color:green">New User :<b>"'.$form['username'].'"</b> Has Been Added Successfully<br>Thanks You !</span>';
				}
				else{
					$data['error']= true;
					$data['msg']  = 'Data Base Error <br> ERROR: usr/ins/100';
				}
			}
			//Edit/Update
			if($this->input->post('id'))
			{
				if($this->user_model->update($id, $form)){
					$data['error']= false;
					$data['msg']= $form['username'].' Successfully Updated..';
				}
				else{
					$data['error']= true;
					$data['msg']  = 'Data Base Error <br> ERROR: usr/upd/101';
				}
			}
			die(json_encode($data));
		}
	}
	
	function form($id=NULL)
	{
		//sleep(1);
		$data='';
		$data['user']='';
		$data['roles']= $this->role_model->where('deleted',0)->find_all();
		
		if(!empty($id))
			$data['user'] = $this->user_model->find($id);
			
		$this->load->view('admin/user/form', $data);
	}
	
			
	function delete($id=NULL)
	{

		
		if (!empty($id))
		{	
			if ($this->user_model->delete($id))
			{
				//success
					$data['msg']	= ' Successfully Deleted..';
					$data['error']	= false;
				}
			else{
				//failed to delete
				$data['error']	= true;
				$data['msg']	= ' Failed to Delete..';
			}
		die(json_encode($data));
		}
		
	}
		
	function get_info($id=NULL){
		if(!$id)
		{
			$user_list = $this->user_model->where('deleted',0)->find_all();
			$contact_group = array();
			//$data['num_row'] = $com_list->num_rows();

			foreach($user_list as $row){
				$user_group[mb_substr($row->first_name,0,1,'utf-8')][] = array("id" => $row->id, "name" => $row->first_name.' '.$row->last_name);
			}
			
			$data['user_list']=$user_group;
		}
		
		if($id)
		{
			sleep(1);
			$data['user_list']  = $this->user_model->find($id);
			
		}

		die(json_encode($data['user_list']));
		
	}
}
